package com.linuxone.tools.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("serial")
public class MenuItemList implements Serializable {
	static final String delimiter = "\\|";
	Map<String, String> ItemList = new HashMap<String, String>();
	
	public MenuItemList(String ListName){
		populateList(getListFile(ListName));
	}

	public Map<String, String> getItemList() {
		return ItemList;
	}
	
	private String getListFile(String ListName) {
		String line;
		String[] myArray;
		BufferedReader buf;
		String fileName = "";
		
		try {
			buf = new BufferedReader(new InputStreamReader(MenuItemList.class.getClassLoader()
					.getResourceAsStream("menuItem/list.txt"), "utf-8"));
			while ((line = buf.readLine()) != null) {
			    myArray = line.split(delimiter);
			    if(myArray[0].compareTo(ListName)==0)
			    	fileName = myArray[1];
			}
		} catch (IOException e) {
			e.printStackTrace();
		}		
		return fileName;
	}
	
	private void populateList(String fileName){
		String line;
		String[] myArray;
		BufferedReader buf;
		
		try {
			buf = new BufferedReader(new InputStreamReader(MenuItemList.class.getClassLoader()
					.getResourceAsStream("menuItem/"+fileName), "utf-8"));
			while ((line = buf.readLine()) != null) {
			    myArray = line.split(delimiter);
			    ItemList.put(myArray[0], myArray[1]);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
